/**
 *
 * jcrt and its source-code is licensed under the LGPL.
 * You may copy, adapt, and redistribute this file for commercial or non-commercial use.
 * When copying, adapting, or redistributing this document in keeping with the guidelines above,
 * you are required to provide proper attribution to obinary.
 * If you reproduce or distribute the document without making any substantive modifications to its content,
 * please use the following attribution line:
 *
 * Copyright 2007 Atjava.com Ltd. (http://www.atjava.com) All rights reserved.
 *
 */
package com.atjava.jcrweb.manager;

import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;

import org.apache.log4j.Logger;
import com.atjava.jcrt.JcrtSession;
import com.atjava.jcrt.JcrtSessionFactory;
import com.atjava.jcrt.core.exception.CoreException;
import com.atjava.jcrweb.JcrWebException;
import com.atjava.jcrweb.info.FileInfo;

/**
 * FileManager
 * 
 * @author biggie
 * 
 */
public class FileManager {
	
	private static FileManager manager = null;
	private JcrtSessionFactory factory = null;
	private JcrtSession jsession = null;
	
	private static Map<String,String> fileIdNames = new HashMap<String,String>();
	/**
	 * Log4j
	 */
	private Logger log = Logger.getLogger(this.getClass().getName());

	public static FileManager getInstance() throws JcrWebException {
		if (manager == null) {
			manager = new FileManager();
		}
		return manager;
	}

	private FileManager() throws JcrWebException {
		try {
			factory = JcrtSessionFactory.getInstance();
			jsession = factory.openJcrtSession();
		} catch (CoreException e) {
			log.error("ContentManager", e);
			throw new JcrWebException(e);
		}
	}

	/**
	 * save single file
	 * 
	 * @param fileInfo
	 * @param path
	 * @throws JcrWebException
	 */
	public void createFile(String path,FileInfo fileInfo) throws JcrWebException {
		try {
			Calendar c = Calendar.getInstance();
			c.setTimeInMillis(System.currentTimeMillis());
			fileInfo.setDatetime(c);	
			jsession.save(fileInfo, path+"/" + fileInfo.getId());
			fileIdNames.put(fileInfo.getFileName(), String.valueOf(fileInfo.getId()));
		} catch (CoreException e) {
			log.error("createFile("+path+"," + fileInfo + ")", e);
			throw new JcrWebException(e);
		} catch (Exception e) {
			log.error("createFile("+path+"," + fileInfo + ")", e);
			throw new JcrWebException(e);
		}
	}
	
	public FileInfo getFile(String path,long id) throws JcrWebException{
		
		try {
			Object obj = jsession.load(FileInfo.class, path+"/"+id);
			if(obj!=null){
				return (FileInfo)obj;
			}
		} catch (CoreException e) {
			log.error("getFile("+path+"," + id + ")", e);
			throw new JcrWebException(e);
		}
		return null;
	}

	/**
	 * delete single File
	 * 
	 * @param path
	 * @throws JcrWebException
	 */
	public void deleteFile(String path,long id) throws JcrWebException {
		try {
			jsession.delete(FileInfo.class, path + "/" + id);
		} catch (CoreException e) {
			log.error("deleteFile("+path+"," + id + ")", e);
		} catch (Exception e) {
			log.error("deleteFile("+path+"," + id + ")", e);
			throw new JcrWebException(e);
		}
	}
	
	public static String getFileIdInMemory(String fileName){
		return fileIdNames.get(fileName);
	}
	public static void clearFileIdMemory(String fileName){
		fileIdNames.remove(fileName);
	}

}
